package com.u21.a0903_onlinemusic.words;


import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.coordinatorlayout.widget.CoordinatorLayout;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.kongzue.dialogx.dialogs.BottomDialog;
import com.kongzue.dialogx.interfaces.OnBindView;
import com.u21.a0903_onlinemusic.R;
import com.u21.a0903_onlinemusic.entity.Music;
import com.u21.a0903_onlinemusic.entity.Word;
import com.u21.a0903_onlinemusic.viewholder.WordViewHolder;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


public class Allwords extends Activity implements OnItemClickListener {


	private ListView lv;
	private WordAdapter adpter;
	private List<String> list = new ArrayList<String>();
	private List<Word> dataSource = new ArrayList<>();
	private FloatingActionButton fab;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wordmain);

		Intent intent = getIntent();
		String str = intent.getStringExtra("Cap");
//		CoordinatorLayout coordinatorLayout = findViewById(R.id.snackbar_container);
//		Snackbar.make(coordinatorLayout, str, Snackbar.LENGTH_SHORT);

		lv = findViewById(R.id.capList);
		fab = findViewById(R.id.fab);


		fab.setOnClickListener(view -> {
			Runtime runtime = Runtime.getRuntime();
			try {
				runtime.exec("input keyevent " + KeyEvent.KEYCODE_BACK);
			} catch (IOException e) {
				e.printStackTrace();
			}
		});


		try {

			InputStream is = getResources().getAssets().open(str + ".txt");

			InputStreamReader isr = new InputStreamReader(is);

			BufferedReader br = new BufferedReader(isr);

			String line = null;

			while ((line = br.readLine()) != null) {

				list.add(line);

				int index = line.indexOf("|");
				String name = line.substring(0, index);
				String explain = line.substring(index + 1);

				dataSource.add(new Word(name, explain));

			}

			br.close();
			is.close();
			isr.close();

		} catch (IOException e) {
			e.printStackTrace();
		}

		adpter = new WordAdapter();
		lv.setAdapter(adpter);
		lv.setOnItemClickListener(this);
	}

	class WordAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return dataSource.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View view, ViewGroup parent) {
			WordViewHolder vh = null;
			if (view == null) {
				view = getLayoutInflater().inflate(R.layout.word_item, null);
				vh = new WordViewHolder(view);
				vh.setData(dataSource, position);
				view.setTag(vh);
			} else {
				vh = (WordViewHolder) view.getTag();
				vh.setData(dataSource, position);
			}
			return view;

		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {

		BottomDialog
				.show(new OnBindView<BottomDialog>(R.layout.activity_randwords) {
					@Override
					public void onBind(BottomDialog dialog, View v) {

						TextView wordMain, wordExp;
						VideoView video;
						String word;
						String name;

						wordMain = v.findViewById(R.id.wordmain);
						wordExp = v.findViewById(R.id.wordexp);
						video = v.findViewById(R.id.video);

						word = list.get(pos);
						int index = word.indexOf("|");

//						video.setMinimumHeight(200);
//						video.setMediaController(new MediaController(getApplicationContext()));
//						video.setVideoURI(Uri.parse(""));


						name = word.substring(0, index);
						String explain = word.substring(index + 1);
						StringBuilder stringBuilder = new StringBuilder(explain);

						String[] sign = {"a.", "v.", "n.", "vi.", "vt.", "ad.", "adj.", "adv.", "pron."};
						for (String s : sign) {
							if (explain.indexOf(s) > 0) {
								stringBuilder.insert(explain.indexOf(s), "\n");
							}
						}

						wordMain.setText(name);
						wordExp.setText(stringBuilder);
						wordMain.setOnClickListener(v1 -> {
							MediaPlayer mediaPlayer = null;
							if (mediaPlayer == null) {
								mediaPlayer = new MediaPlayer();

							}
							//为了实现多媒体同一时间只播放一首音乐，我们必须对多媒体对象做同步处理
							synchronized (mediaPlayer) {
								//实现多媒体对音乐的播放
								try {
									//设置播放音乐源—播放音乐的网络链接
									mediaPlayer.setDataSource("https://dict.youdao.com/dictvoice?audio=" + name);
									//播放器做播放准备
									mediaPlayer.prepare();
									//获取到音乐播放总时间

									mediaPlayer.start();

								} catch (Exception e) {
									e.printStackTrace();
								}


							}
						});

					}
				});
//		Intent intent = new Intent(this, WordActivity.class);
//		intent.putExtra("Word", list.get(pos));
//		startActivity(intent);
	}

}

	
